import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
var goods = JSON.parse(localStorage.getItem('goods') || '[]')
export default new Vuex.Store({
    state: {
        car: goods
    },
    mutations: {
        goCarShop(state, goods) {
            var flag = false;
            state.car.some(item => {
                if (item.id == goods.id) {
                    item.count += goods.count;
                    flag = true;
                    return true;
                }

            })
            if (!flag) {
                state.car.push(goods)
            }
            localStorage.setItem('goods', JSON.stringify(state.car))
        },
        resetCarCount(state,goods){
            state.car.some(item=>{
                if(item.id==goods.id){
                    item.count=goods.count;
                    return true;
                }
            })
            localStorage.setItem('goods',JSON.stringify(state.car))
        }

    }
})